//index.js
//获取应用实例
var scene
const app = getApp()
var storage
let level_js = require('../../pages/index/level.js')
var isPK 
let level
let roomid
let gameid

Page({
  data: {
    buttonClicked: true,
    userInformation: true,
    level_item: {
      name: "",
      ratio: 0.0,
      str: ""
    }
  },
  toNewGame() {
      if (app.globalData.userInfo){
          try {
              var data = wx.getStorageSync('cache')
              if (data != '') {
                  wx.showModal({
                      title: '提示',
                      content: '您有未完成的游戏，是否继续游戏',
                      cancelText: '继续游戏',
                      confirmText: '新建游戏',
                      success: function (res) {
                          if (res.confirm) {
                              wx.navigateTo({
                                  url: '/pages/level_select/level_select',
                              })
                          } else if (res.cancel) {
                              wx.navigateTo({
                                  url: '/pages/sudoku/sudoku?cache=' + data,
                              })
                          }
                      }
                  })
              } else {
                  wx.getNetworkType({
                      success: function (res) {
                          wx.navigateTo({
                              url: '/pages/level_select/level_select',
                          })
                      },
                  })
              }
          } catch (e) {
              console.log(e)  //缓存上局游戏的时候的报错
          }
      } else{
          this.setData({
              userInformation: false
          })
          wx.showModal({
              title: '糟糕',
              content: '为了制作专属成就卡片，请您授权个人信息',
              showCancel: false,
              confirmText: "知道了"
          })
      }
    
  },
  toHelp() {
    if (!this.data.buttonClicked) { return }
    buttonClicked(this);
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  onShow: function(){
      var item = level_js.level_ratio()
      this.setData({
          level_item: item
      })
  },
  onLoad: function (options) {
    var item = level_js.level_ratio()
    this.setData({
        level_item: item
    })

    scene = decodeURIComponent(options.scene)
    var that = this
    
  },
  showInfo: function(){
      wx.showModal({
          title: '新功能',
          content: '还未开发，请稍后',
          cancelText: "确定",
          cancelColor: "#000",
          confirmText: "取消",
          confirmColor: "#000"
      })
    },
    onShareAppMessage: function () {
        let picNum = Math.floor(Math.random() * 10)+1
        let url = 'https://www.tianzhipengfei.xin/static/share' + picNum.toString() + '.jpg'
        return {
            title: '来啊造作啊',
            path: '/pages/index/index', 
            imageUrl: url,
            success: function (res) {
            },
            
            fail: function (res) {
                wx.showToast({
                    title: '分享失败',
                    icon: 'none',
                    duration: 1000
                })
            }
        }
    },
    bindgetuserinfo: function (e) {
        var that = this;
        console.log(e)
        if (e.detail.userInfo) {
            app.globalData.userInfo = e.detail.userInfo
            that.setData({
                userInformation: true
            })
        } else {
            console.log(333, '执行到这里，说明拒绝了授权')
            wx.showToast({
                title: "为了您更好的体验,请先同意授权",
                icon: 'none',
                duration: 2000
            });
        }
    }
})

var buttonClicked = function (that) {
  that.setData({
    buttonClicked: false
  })
  setTimeout(function () {
    that.setData({
      buttonClicked: true
    })
  }, 1000);
}